import cache from '@/plugins/cache'

const TokenName = 'tokenName'
const TokenValue = 'tokenValue'
const RememberMe = 'rememberMe'

export function getTokenName() {
    if (cache.local.get(RememberMe) === "true") {
        return cache.local.get(TokenName)
    }
    return cache.session.get(TokenName)
}

export function getTokenValue() {
    if (cache.local.get(RememberMe) === "true") {
        return cache.local.get(TokenValue)
    }
    return cache.session.get(TokenValue)
}

export function removeToken() {
    cache.local.remove(TokenName)
    cache.local.remove(TokenValue)
    cache.session.remove(TokenName)
    cache.session.remove(TokenValue)
}

export function setTokenInfo(tokenName, tokenValue) {
    if (cache.local.get(RememberMe) === "true") {
        cache.local.set(TokenName, tokenName)
        cache.local.set(TokenValue, tokenValue)
    } else {
        cache.session.set(TokenName, tokenName)
        cache.session.set(TokenValue, tokenValue)
    }
}

export function setTokenHeader(header) {
    header = header || {};
    if (getTokenName()) {
        return header[getTokenName()] = 'Bearer ' + getTokenValue();
    }
    return header;
}



